<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_user_contacts extends user_contacts{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
    /*
	*	取出一条信息
	*/
	public function do_detail($uc_id='') {
        global $_G;
		$detail = array();
        $user_contacts = isset($_REQUEST['user_contacts']) ? $_REQUEST['user_contacts'] : '';
		$detail = $this->one_info('user_contacts','uc_id',$user_contacts['uc_id']);
        $user = $this->one_info('user','user_id',$detail['uc_user_id']);
        $user_detail = $this->one_info('user_detail','user_id',$detail['uc_user_id']);
        if(!empty($user_contacts) &&!empty($detail) && !empty($user) && !empty($user_detail)){
            //DEBUG 联系人信息
            $return['data']['user']['contacts']['detail']['uc_id']=$detail['uc_id'];
            $return['data']['user']['contacts']['detail']['uc_user_id']=$detail['uc_user_id'];
            $return['data']['user']['contacts']['detail']['user_avatar']=$user_detail['user_avatar'];
            $return['data']['user']['contacts']['detail']['gender']=$user_detail['gender'];
            $return['data']['user']['contacts']['detail']['user_name']=$user['user_name'];
            $return['data']['user']['contacts']['detail']['user_nickname']=$detail['uc_byname'];
            $return['data']['user']['contacts']['detail']['user_sign']=$user_detail['user_sign'];
            $return['data']['user']['contacts']['detail']['uc_other']=$detail['uc_other'];
            $return['data']['user']['contacts']['detail']['uc_tel']=$detail['uc_tel'];   
        }
        if(!empty($_G['gp_api']) && !empty($return['data'])){
            $return['code'] = '0';
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $return['code'] = '1';
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }
	}
    
	/*
	*  添加系统信息
	*/
	public function do_add() {
        global $_G;
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
		$user_contacts = isset($_REQUEST['user_contacts']) && !empty($_REQUEST['user_contacts']) ? $_REQUEST['user_contacts']:'';
		//TODO 后端字段校验 根据具体业务逻辑添加
        //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
        foreach($user_contacts AS $key => $value){
            if(is_array($value) && !empty($value)){
                $user_contacts[$key] = implode(",", $value);
            }
        }
        //DEBUG 检测用户是否存在
        $check_user_exist = 0;
        if(!empty($user_contacts['uc_user_id'])){
            
        }
        $insert_id='';
        if(!empty($user_contacts) && $user_contacts['uc_user_id']){
            $user_info = ext::getuserbyuid($user_contacts['uc_user_id'],0,1);
            $user_contacts['uc_tel']=$user_info['user_detail']['user_phone'];
            $user_contacts['uc_byname']=$user_info['user_detail']['user_nickname'];
            $user_contacts['user_id']=$user_id;
            $user_contacts['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('user_contacts',$user_contacts);   
        }
        if($insert_id){
            if(!empty($_G['gp_api'])){
                $return['code'] = '0';
                echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{
                showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
            }
        }else{
            if(!empty($_G['gp_api'])){
                $return['code'] = '1';
                echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{
                showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
            }
        }
	}
    
    /*
	*  确认好有
	*/
	public function do_confirm() {
        global $_G;
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
		$user_contacts = isset($_REQUEST['user_contacts']) && !empty($_REQUEST['user_contacts']) ? $_REQUEST['user_contacts']:'';
		//TODO 后端字段校验 根据具体业务逻辑添加
        //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
        foreach($user_contacts AS $key => $value){
            if(is_array($value) && !empty($value)){
                $user_contacts[$key] = implode(",", $value);
            }
        }
        //DEBUG 检测用户是否存在
        $check_user_exist = 0;
        if(!empty($user_contacts['uc_user_id'])){
            
        }
        $insert_id='';
        if(!empty($user_contacts) && $user_contacts['uc_user_id'] && $user_id){
            $user_contacts['modify_dateline'] = TIMESTAMP;
            $update = array('uc_accept'=>0);
            $where = array('user_id'=>$user_id,'uc_user_id'=>$user_contacts['uc_user_id']);
            $update_id = $this->edit('user_contacts',$update,$where);
            if(!empty($_G['gp_api'])){
                $return['code'] = '0';
                echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{
                showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
            }
        }else{
            if(!empty($_G['gp_api'])){
                $return['code'] = '1';
                echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
            }else{
                showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
            }
        }
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$user_contacts = isset($_REQUEST['user_contacts']) && !empty($_REQUEST['user_contacts']) ? $_REQUEST['user_contacts']:'';
        $uc_id = isset($_REQUEST['uc_id']) ? $_REQUEST['uc_id'] : '';
        if($uc_id){
            if('1'==$issubmit){
                $user_contacts['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($user_contacts AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $user_contacts[$key] = implode(",", $value);
                    }
                }                
                $where = array('uc_id'=>$uc_id);
                $effect_row = $this->edit('user_contacts',$user_contacts,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=user&action=contacts&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('user_contacts','uc_id',$uc_id);
                include template('user/contacts/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=user&action=contacts&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $uc_id = isset($_REQUEST['uc_id']) ? $_REQUEST['uc_id'] : '';
        if($uc_id){
            $effect_row = $this->delete("user_contacts", array("uc_id" => $uc_id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=user&action=contacts&do=index');
            }else{
                showmessage('操作失败','index.php?mod=user&action=contacts&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 2000;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql .= " AND uc.uc_tel LIKE '%".$keyword."%' ";
        }
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
        if($user_id){
            $wheresql .= " AND uc.user_id = '".$user_id."' ";
        }
        $uc_accept = isset($_REQUEST['uc_accept']) && !empty($_REQUEST['uc_accept']) ? $_REQUEST['uc_accept']:'';
        if($uc_accept !=''){
            $wheresql .= " AND uc.uc_accept = '".$uc_accept."' ";
        }
        $orderby = " ORDER BY uc.uc_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('user_contacts',$page_condition);
        $page_result['timestamp']=TIMESTAMP;
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=contacts&do=index&keyword=".$keyword);
        foreach($page_result['page_data'] AS $key => $value){
            //$value['user_name'] = ext::user_nickname($value['uc_user_id']);
            $page_result['page_data']['uc_accept_'.$value['uc_accept']][]=$value;
            unset($page_result['page_data'][$key]);
        }
        if(!empty($_G['gp_api']) && $user_id){
            $return['code'] = '0';
            $return['data']['user']['contacts']['index'] = $page_result;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $return['code'] = '1';
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }	
	}
    
    /*
	*  搜索用户
	*/
	public function do_search(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 5000;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
        if($keyword && $user_id){
            $wheresql = " AND (ud.user_nickname LIKE '%".$keyword."%' || um.user_name='".$keyword."') AND ud.user_id <> '".$_G['user_id']."' ";
            //DEUBG 取出自己的好友
            if($_G['user_id'] || $user_id){
                $sql = "SELECT uc_user_id FROM ".DB::table('user_contacts')." WHERE user_id='".$_G['user_id']."' || user_id='".$user_id."' LIMIT 5000";
                $result = DB::fetch_all($sql);
                foreach($result AS $key=>$value){
                   $uc_user_ids[$value['uc_user_id']]=$value['uc_user_id'];
                }
                if(!empty($uc_user_ids)){
                    $wheresql .= " AND ud.user_id NOT IN (".dimplode($uc_user_ids).") ";
                }
            }
            //DEBUG  AND ud.user_id <> '".$user_id."'
            $wheresql .= " AND um.user_id <> '".$user_id."' ";
            $orderby = " ORDER BY ud.user_id DESC ";
            //DEBUG 初始化请求请求获取一页列表数据的参数
            $page_condition=array(
                'page' => $page,//int 请求页面 页码
                'limit' => $limit,//int 每页请求个数
                'perpage' => $perpage,//int 每页显示个数
                'wheresql' => $wheresql,//string //条件SQL语句 
                'orderby' => $orderby,//string 排序规则
            );
            //print_r($page_condition);
            //DEBUG 列表数据返回结构
            /*
            $page_result = array(
                //int 返回结果总数
                'total_rows' => $total_rows,
                //array 一页数据数组
                'page_data' => $page_data 
            );
            */
            $page_result = $this->search('user_detail',$page_condition,'ud.user_id,um.user_name,ud.user_nickname,ud.user_avatar');
            //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=user&action=contacts&do=index&keyword=".$keyword);
            //foreach($page_result['page_data'] AS $key => $value){
                //$user_info = ext::getuserbyuid($value['user_id']);
                //$page_result['page_data'][$key]['user_name'] = $user_info['user_name'];
            //}
            if(!empty($_G['gp_api'])){
                $return['code'] = '0';
                $return['data']['user']['contacts']['search'] = $page_result;
            }else{
                $return['code'] = '1';
            }
        }else{
            $return['code'] = '2';
        }
        echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY uc_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('user_contacts',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>